"""
Working search integration tests without complex mocking
"""

import pytest
from httpx import AsyncClient, ASGITransport
import sys
import os

# Set test environment variables
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

os.environ["SECRET_KEY"] = "test-secret-key-for-testing-only"
os.environ["ENVIRONMENT"] = "testing"
os.environ["DEBUG"] = "true"

from main import app


@pytest.mark.asyncio
async def test_search_success():
    """Test successful search request"""
    search_query = {
        "query": "test",
        "filters": {},
        "include_ukm": True,
        "include_uma": True,
        "include_enums": True
    }

    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post("/api/v1/search/", json=search_query)

        # Should not return unexpected errors
        assert response.status_code in [200, 422, 500]


@pytest.mark.asyncio
async def test_search_with_pagination():
    """Test search with pagination parameters"""
    search_query = {
        "query": "test",
        "filters": {},
        "include_ukm": True,
        "include_uma": True,
        "include_enums": True,
        "page": 1,
        "page_size": 5
    }

    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post("/api/v1/search/", json=search_query)

        # Should not return unexpected errors
        assert response.status_code in [200, 422, 500]


@pytest.mark.asyncio
async def test_search_with_filters():
    """Test search with filters"""
    search_query = {
        "query": "test",
        "filters": {
            "type": ["histogram"],
            "component": ["TestComponent"]
        },
        "include_ukm": True,
        "include_uma": True,
        "include_enums": True
    }

    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post("/api/v1/search/", json=search_query)

        # Should not return unexpected errors
        assert response.status_code in [200, 422, 500]


@pytest.mark.asyncio
async def test_get_suggestions():
    """Test getting search suggestions"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        params = {"q": "test", "limit": 10}
        response = await client.get("/api/v1/search/suggestions", params=params)

        # Should not return unexpected errors
        assert response.status_code in [200, 422, 500]


@pytest.mark.asyncio
async def test_get_components():
    """Test getting components list"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        params = {"limit": 20}
        response = await client.get("/api/v1/search/components", params=params)

        # Should not return unexpected errors
        assert response.status_code in [200, 422, 500]


@pytest.mark.asyncio
async def test_get_tokens():
    """Test getting tokens list"""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        params = {"limit": 20}
        response = await client.get("/api/v1/search/tokens", params=params)

        # Should not return unexpected errors
        assert response.status_code in [200, 422, 500]


@pytest.mark.asyncio
async def test_search_no_results():
    """Test search that returns no results"""
    search_query = {
        "query": "nonexistentmetric12345",
        "filters": {},
        "include_ukm": True,
        "include_uma": True,
        "include_enums": True
    }

    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post("/api/v1/search/", json=search_query)

        # Should not return unexpected errors
        assert response.status_code in [200, 422, 500]


@pytest.mark.asyncio
async def test_search_large_page_size():
    """Test search with large page size"""
    search_query = {
        "query": "test",
        "filters": {},
        "include_ukm": True,
        "include_uma": True,
        "include_enums": True,
        "page_size": 1000
    }

    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post("/api/v1/search/", json=search_query)

        # Should not return unexpected errors
        assert response.status_code in [200, 422, 500]


@pytest.mark.asyncio
async def test_search_unauthorized():
    """Test search endpoints without authentication"""
    search_query = {
        "query": "test",
        "filters": {},
        "include_ukm": True,
        "include_uma": True,
        "include_enums": True
    }

    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post("/api/v1/search/", json=search_query)

        # Should not return unexpected errors
        assert response.status_code in [200, 422, 500]